package com.cpi.cornpp.version.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.monthsperiods.services.MonthsPeriodsService;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.form.VersionForm;
import com.cpi.cornpp.version.services.VersionService;

public class VersionListAction extends Action{

	private final static String DEFAULT = "default";
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		VersionService service = null;
		List listVersionViews = null;
		VersionForm versionForm = null;
		HttpSession httpSession = null;
		int orderField = 0;
		int orderType = 0;
		
		int actualMonth = 0;
		MonthsPeriodsService monthPeriodService = null;
		VersionService versionService = null;
		List monthPeriodsView = null;
		List versionsView = null;		
		try{
			listVersionViews = new ArrayList(0);
			service = new VersionService();
			
			versionForm = (VersionForm) form;
			httpSession = request.getSession(false);
			monthPeriodService = new MonthsPeriodsService();
			versionService = new VersionService();
			
			actualMonth = monthPeriodService.actualMonthPeriod();
			monthPeriodsView = monthPeriodService.listOfMonthsPeriods();
			versionsView = versionService.list(0, true);
			
			request.setAttribute("actualMonth", new Integer(actualMonth));
			request.setAttribute("actualYear", new Integer(Calendar.getInstance().get(Calendar.YEAR)));
			request.setAttribute("monthPeriods", monthPeriodsView);
			request.setAttribute("versions", versionsView);
			
			if(form != null){
				orderField 	= versionForm.getOrderField();
				orderType 	= versionForm.getOrderType();
			}
			listVersionViews = service.list(orderField,(1 == orderType) ? false : true);
			request.setAttribute("listVersionViews", listVersionViews);
		}catch (VersionException versionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}
		catch (Exception e){
			VersionException versionException=null;
			e.printStackTrace(System.out);
			versionException= new VersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		} finally {
			httpSession.setAttribute("previousOrderField", Integer.toString(versionForm.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(versionForm.getOrderType()));
			httpSession.removeAttribute("versionId");
		}
		return mapping.findForward(DEFAULT);
	}
}
